package com.feonix.complat.controller.configuration;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.feonix.common.BaseInfo;
import com.feonix.common.annotation.Permission;
import com.feonix.common.util.Properties;
import com.feonix.common.util.mvc.JsonResult;
import com.feonix.common.util.mvc.ResultState;
import com.feonix.complat.constant.Settings;

/**
 * 系统参数设置控制器
 * 
 * @author 李杰
 * 
 */
@Controller
@Permission(module = "configuration")
@RequestMapping("manager/main/configuration")
public class ConfigurationController {

	/**
	 * 打开系统参数设置页面
	 * 
	 * @return
	 */
	@RequestMapping("modify_show")
	public ModelAndView showModify() {
		ModelAndView modelAndView = new ModelAndView("/complat/configuration/configuration_opr");
		modelAndView.addObject("setting", Settings.getSettings());
		modelAndView.addObject("url", "update_submit.do");
		return modelAndView;
	}

	/**
	 * 保存系统参数
	 * 
	 * @param settings
	 *            系统参数实体
	 * @return
	 */
	@Permission(function = "update_submit")
	@RequestMapping("update_submit")
	@ResponseBody
	public JsonResult submitUpdate(Settings settings) {
		Settings.getSettings().setBanTimes(settings.getBanTimes());
		Settings.getSettings().setCheckLevel(settings.getCheckLevel());
		Settings.getSettings().setEnableVerifyCode(settings.isEnableVerifyCode());
		Settings.getSettings().setFileTmp(settings.getFileTmp());
		Settings.getSettings().setImageDir(settings.getImageDir());
		Settings.getSettings().setAttachmentDir(settings.getAttachmentDir());
		Settings.getSettings().setLoginError(settings.getLoginError());
		Settings.getSettings().setSessionTime(settings.getSessionTime());
		// Settings.getSettings().setCanFeedback(canFeedback)
		// Settings.getSettings().setInterceptMode(1);
		Properties properties = new Properties(BaseInfo.getClassPath()
				+ "pub/config/complat.properties");
		properties.setProperty("interceptMode", Settings.getSettings().getInterceptMode());
		properties.setProperty("loginError", Settings.getSettings().getLoginError());
		properties.setProperty("banTimes", Settings.getSettings().getBanTimes());
		properties.setProperty("canFeedback", Settings.getSettings().isCanFeedback());
		properties.setProperty("checkLevel", Settings.getSettings().getCheckLevel());
		properties.setProperty("sessionTime", Settings.getSettings().getSessionTime());
		properties.setProperty("fileTmp", Settings.getSettings().getFileTmp());
		properties.setProperty("imageDir", Settings.getSettings().getImageDir());
		properties.setProperty("attachmentDir", Settings.getSettings().getAttachmentDir());
		properties.setProperty("enableVerifyCode", Settings.getSettings().isEnableVerifyCode());
		properties.save();
		JsonResult jsonResult = JsonResult.getInstance();
		jsonResult.set(ResultState.MODIFY_SUCCESS);
		return jsonResult;
	}
}
